using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Sirenix.OdinInspector;
using UnityEngine.UI;
using UnityEditor;

[CreateAssetMenu(fileName = "SkillObject", menuName = "SkillObject/SkillExecution")]
public class SkillObject : ScriptableObject
{
    [DelayedProperty]
    public string Id;
    public float TotalTime;
    [DelayedProperty]
    public GameObject ObjAsset;
    public ExecuteTargetInputType TargetInputType;
    [ShowIf("TargetInputType", ExecuteTargetInputType.Point)]
    [LabelText("范围指示器")]
    public GameObject RangeIndicator;

    [ShowIf("TargetInputType", ExecuteTargetInputType.Point)]
    [LabelText("目标点指示器")]
    public GameObject PointIndicator;

    [ShowIf("TargetInputType", ExecuteTargetInputType.Point)]
    [LabelText("朝向指示器")]
    public GameObject DirectionIndicator;

    [ReadOnly, Space(10)]
    public List<SkillClipData> SkillClips = new List<SkillClipData>();

#if UNITY_EDITOR

    void SaveClips()
    {
        EditorUtility.SetDirty(this);
        // AssetDatabase.SaveAssetIfDirty(this);
        AssetDatabase.SaveAssets();
    }

    void BeginBox()
    {
        GUILayout.Space(20);
        if(GUILayout.Button("Save Clips"))
        {
            SaveClips();
        }

        GUILayout.Space(10);
        Sirenix.Utilities.Editor.SirenixEditorGUI.DrawThickHorizontalSeparator();
        GUILayout.Space(10);
    }

    // [OnInspectorGUI]
    // [SerializeField,LabelText("自动重命名")]
    // public bool AutoRename { get { return BuffConfigObject.AutoRename; } set { BuffConfigObject.AutoRename = value; } }

    void  OnEnable()
    {
        // BuffConfigObject.AutoRename = UnityEditor.EditorPrefs.GetBool("AutoRename", true);
    }

    void OnDisable()
    {
        // UnityEditor.EditorPrefs.SetBool("AutoRename", BuffConfigObject.AutoRename);
    }

    [OnInspectorGUI]
    void OnInspectorGUI()
    {
        // if (!AutoRename)
        // {
        //     return;
        // }

        // RenameFile();
    }

    // [Button("重命名配置文件"), HideIf("AutoRename")]
    private void RenameFile()
    {
        string[] guids = UnityEditor.Selection.assetGUIDs;
        int i = guids.Length;
        if (i == 1)
        {
            string guid = guids[0];
            string assetPath = UnityEditor.AssetDatabase.GUIDToAssetPath(guid);
            var so = UnityEditor.AssetDatabase.LoadAssetAtPath<SkillObject>(assetPath);
            if (so != this)
            {
                return;
            }
            var fileName = System.IO.Path.GetFileName(assetPath);
            var newName = $"Execution_{this.Id}";
            if (!fileName.StartsWith(newName))
            {
                UnityEditor.AssetDatabase.RenameAsset(assetPath, newName);
            }
        }
    }

#endif
}
